import 'package:flutter/material.dart';

// 自定义的一个文本按钮
class MyTextButton extends StatefulWidget {
  final String data; // 文本字符串
  final VoidCallback onPressed; // 点击事件回调
  final Color? backgroundColor; // 按钮背景颜色
  final Color? textColor; // 文字颜色
  final EdgeInsetsGeometry? padding; // 内边距
  final BorderRadiusGeometry? radius; // 圆角
  final FontWeight? fontWeight; // 字体是否加粗
  final double? height; // 容器高度
  final double? width; // 容器宽度

  const MyTextButton(this.data, {
    super.key,
    required this.onPressed,
    this.backgroundColor,
    this.padding,
    this.radius,
    this.textColor, this.fontWeight, this.height, this.width,
  });

  @override
  State<MyTextButton> createState() => _MyTextButtonState();
}

class _MyTextButtonState extends State<MyTextButton> {
  bool _isHighlighted = false; // 按钮是否被点击
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (_) {
        setState(() {
          _isHighlighted = true;
        });
      },
      onTapUp: (_) {
        // 抬起时取消高亮
        setState(() {
          _isHighlighted = false;
        });
        // 调用父组件的回调
        widget.onPressed();
      },
      onTapCancel: () {
        // 取消时取消高亮
        setState(() {
          _isHighlighted = false;
        });
      },
      child: Container(
        height: widget.height ?? 30,
        width: widget.width ??  80,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: _isHighlighted
              ? widget.backgroundColor?.withOpacity(0.5) ??
              const Color(0xffac5c2a).withOpacity(0.5)
              : widget.backgroundColor ?? const Color(0xffac5c2a),
          borderRadius:
          widget.radius ?? BorderRadius.circular(6), // 有传值的话用传的，没有用默认值
        ),
        child: Text(
          widget.data,
          style: TextStyle(
            color: widget.textColor ?? Colors.white,
            fontWeight: widget.fontWeight ?? FontWeight.bold,
          ),
        ),
      ),
    );
  }
}